package cn.appsys.service.deve.appversion;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.appsys.dao.deve.appversion.DevAppVersionMapper;
import cn.appsys.pojo.AppVersion;

/**
 * APP版本表实体类业务接口实现类
 * @author Administrator
 *
 */
@Service("devAppVersionService")
public class DevAppVersionServiceImpl implements DevAppVersionService{

	@Resource
	private DevAppVersionMapper devAppVersionMapper;
	
	@Override
	public List<AppVersion> getAppVersionById(Integer id) {
		// TODO Auto-generated method stub
		return devAppVersionMapper.getAppVersionById(id);
	}

	@Override
	public boolean insertAppVersion(AppVersion appVersion) {
		boolean isOk = false;
		if (devAppVersionMapper.insertAppVersion(appVersion)>0) {
			isOk = true;
		}
		return isOk;
	}

	@Override
	public AppVersion getAppVersionByVid(Integer vid) {
		// TODO Auto-generated method stub
		return devAppVersionMapper.getAppVersionByVid(vid);
	}

	@Override
	public int upApkFile(Integer id) {
		AppVersion appVersion = devAppVersionMapper.getAppVersionByVid(id);
		if (appVersion!=null&&appVersion.getApkLocPath()!=null) {
			//通过apk文件的绝对路径创建文件对象
			File file = new File(appVersion.getApkLocPath());
			if (file.exists()) {//判断文件是否存在
				file.delete();//存在就删除
			}
		}
		return devAppVersionMapper.upApkFile(id);
	}

	@Override
	public int modifyAppVersion(AppVersion appVersion) {
		// TODO Auto-generated method stub
		return devAppVersionMapper.modifyAppVersion(appVersion);
	}

	@Override
	public boolean delAppVersionByAppId(Integer appId) {
		//通过appid查询该app下的所有版本信息列表
		List<AppVersion> list = devAppVersionMapper.getAppVersionById(appId);
		//遍历集合，循环删除文件
		for (int i = 0; i < list.size(); i++) {
			//通过list集合下标获取appVersion对象，判断绝对路径是否存在
			if (list.get(i).getApkLocPath()!=null) {
				//通过apk文件的绝对路径创建文件对象
				File file = new File(list.get(i).getApkLocPath());
				if (file.exists()) {//判断文件是否存在
					//存在就删除
					file.delete();
				}
			}
		}
		return (devAppVersionMapper.delAppVersionByAppId(appId)>0);
	}

}
